package com.esun.payment_api.controller;


import com.esun.common.api.CommonPage;
import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.*;
import com.esun.payment_api.dao.DkReceiveDao;
import com.esun.payment_api.domain.*;
import com.esun.payment_api.security.model.LoginUser;
import com.esun.payment_api.service.*;
import org.apache.catalina.Session;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/claim")
public class DkClaimController extends BaseController {

    @Resource
    private DkClaimService dkClaimService;

    @Resource
    private InvFormService invFormService;

    @Resource
    private SysDictContractService sysDictContractService;

    @Resource
    private SysDictProjectService sysDictProjectService;

    @Resource
    private SysDictDataService sysDictDataService;

    @Resource
    private LogService logService;

    final String moduleName = "到款认领";
    final Boolean defaultUser = false; //无token时是否使用测试用户，仅供测试使用

    public User testUser(){
        System.out.println("getLoginUser未成功，使用默认值");
        User user = new User();
        user.setId((short)64);
        user.setCoId((short)22);
        user.setDeptId((short)44);
        return user;
    }

    //获取可认领发票清单
    @GetMapping("/claimSelectInvoice")
    public CommonResult claimSelectInvoice(Short companyId, Integer claimId, Integer customerId, String searchStr,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        if(companyId==null){
            return CommonResult.failed("公司ID不能为空");
        }
        if(claimId==null){
            return CommonResult.failed("认领ID不能为空");
        }
        List<DkInvoiceBean> list = invFormService.claimSelectInvoice(companyId, claimId, customerId, searchStr, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list), "获取可认领发票成功");

    }

    //获取可认领合同清单
    @GetMapping("/claimSelectContract")
    public CommonResult claimSelectContract(Short companyId, Integer claimId, Integer customerId, String searchStr,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        if(companyId==null){
            return CommonResult.failed("公司ID不能为空");
        }
        if(claimId==null){
            return CommonResult.failed("认领ID不能为空");
        }
        List<DkContractBean> list = sysDictContractService.claimSelectContract(companyId, claimId, customerId, searchStr, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list), "获取可认领合同成功");

    }

    //获取可认领任务号清单
    @GetMapping("/claimSelectTask")
    public CommonResult claimSelectTask(Short companyId, Integer claimId, String year, String searchStr,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        if(companyId==null){
            return CommonResult.failed("公司ID不能为空");
        }
        if(claimId==null){
            return CommonResult.failed("认领ID不能为空");
        }
        List<DkTaskBean> list = sysDictProjectService.claimSelectTask(companyId, claimId, year, searchStr, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(list), "获取可认领任务号成功");

    }

    //认领到款 -- 用于初次认领
    @RequestMapping("/claimReceive")
    public CommonResult claimReceive(Integer receiveId){
        if(BlankUtils.isBlank(receiveId)){
            return CommonResult.failed("到款ID(receiveId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.claimReceive(receiveId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //暂存认领
    @RequestMapping("/saveClaim")
    public CommonResult saveClaim(@RequestBody DkClaim dkClaim){
        if(dkClaim==null){
            return CommonResult.failed("未收到认领记录");
        }
//        if(dkClaim.getReceiveId()==null){
//            return CommonResult.failed("未收到到款记录ID(receiveId)");
//        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.saveClaim(dkClaim, user);
        if(!BlankUtils.isBlank(dkClaim.getId())){
            dkClaimService.autoCalMoney(dkClaim.getId());
        }
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //提交认领
    @RequestMapping("/submitClaim")
    public CommonResult submitClaim(@RequestBody DkClaim dkClaim){
        if(dkClaim==null){
            return CommonResult.failed("未收到认领记录");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.submitClaim(dkClaim, user);
//        if(!BlankUtils.isBlank(dkClaim.getId())){
//            dkClaimService.autoCalMoney(dkClaim.getId());
//        }
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //重新提交认领
    @RequestMapping("/resubmitClaim")
    public CommonResult resubmitClaim(@RequestBody DkClaim dkClaim){
        if(dkClaim==null){
            return CommonResult.failed("未收到认领记录");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.resubmitClaim(dkClaim, user);
//        if(!BlankUtils.isBlank(dkClaim.getId())){
//            dkClaimService.autoCalMoney(dkClaim.getId());
//        }
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //取消认领
    @RequestMapping("/cancelClaim")
    public CommonResult cancelClaim(Integer claimId){
        if(claimId==null){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.cancelClaim(claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //认领关联发票
    @RequestMapping("/claimRelateInvoice")
    public CommonResult claimRelateInvoice(String invoiceStr, Integer claimId){
        if(BlankUtils.isBlank(invoiceStr)){
            return CommonResult.failed("未收到发票记录ID");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        invoiceStr = invoiceStr.replaceAll(",",":");
        CommonResult commonResult = dkClaimService.claimRelateInvoice(invoiceStr, claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //认领关联合同
    @RequestMapping("/claimRelateContract")
    public CommonResult claimRelateContract(String contractStr, Integer claimId){
        if(BlankUtils.isBlank(contractStr)){
            return CommonResult.failed("未收到合同记录ID");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        contractStr = contractStr.replaceAll(",",":");
        CommonResult commonResult = dkClaimService.claimRelateContract(contractStr, claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //认领关联任务号
    @RequestMapping("/claimRelateTask")
    public CommonResult claimRelateTask(String taskStr, Integer claimId){
        if(BlankUtils.isBlank(taskStr)){
            return CommonResult.failed("未收到任务记录ID");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        taskStr = taskStr.replaceAll(",",":");
        CommonResult commonResult = dkClaimService.claimRelateTask(taskStr, claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //认领取消关联发票
    @RequestMapping("/claimCancelInvoice")
    public CommonResult claimCancelInvoice(String invoiceStr, Integer claimId){
        if(BlankUtils.isBlank(invoiceStr)){
            return CommonResult.failed("未收到合同记录ID");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        invoiceStr = invoiceStr.replaceAll(",",":");
        CommonResult commonResult = dkClaimService.claimCancelInvoice(invoiceStr, claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //认领取消关联合同
    @RequestMapping("/claimCancelContract")
    public CommonResult claimCancelContract(String contractStr, Integer claimId){
        if(BlankUtils.isBlank(contractStr)){
            return CommonResult.failed("未收到合同记录ID");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        contractStr = contractStr.replaceAll(",",":");
        CommonResult commonResult = dkClaimService.claimCancelContract(contractStr, claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //认领取消关联任务号
    @RequestMapping("/claimCancelTask")
    public CommonResult claimCancelTask(String taskStr, Integer claimId){
        if(BlankUtils.isBlank(taskStr)){
            return CommonResult.failed("未收到任务号记录ID");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领记录ID");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        taskStr = taskStr.replaceAll(",",":");
        CommonResult commonResult = dkClaimService.claimCancelTask(taskStr, claimId, user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }


    //编辑认领关联发票
    @RequestMapping("/editClaimInvoice")
    public CommonResult editClaimInvoice(@RequestBody DkClaimInvoice dkClaimInvoice){
        if(dkClaimInvoice==null){
            return CommonResult.failed("未收到认领发票记录");
        }
        if(dkClaimInvoice.getId()==null){
            return CommonResult.failed("未收到认领发票记录的ID");
        }
        CommonResult commonResult = dkClaimService.editClaimInvoice(dkClaimInvoice);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //编辑认领关联合同
    @RequestMapping("/editClaimContract")
    public CommonResult editClaimContract(@RequestBody DkClaimContract dkClaimContract){
        if(dkClaimContract==null){
            return CommonResult.failed("未收到认领合同记录");
        }
        if(dkClaimContract.getId()==null){
            return CommonResult.failed("未收到认领合同记录的ID");
        }
        CommonResult commonResult = dkClaimService.editClaimContract(dkClaimContract);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //编辑认领关联任务号
    @RequestMapping("/editClaimTask")
    public CommonResult editClaimTask(@RequestBody DkClaimTask dkClaimTask){
        if(dkClaimTask==null){
            return CommonResult.failed("未收到认领任务号记录");
        }
        if(dkClaimTask.getId()==null){
            return CommonResult.failed("未收到认领任务号记录的ID");
        }
        CommonResult commonResult = dkClaimService.editClaimTask(dkClaimTask);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //获取待认领到款记录清单
    @GetMapping("/getUnclaimedList")
    @ResponseBody
    public CommonResult getUnclaimedList(String searchStr, String monthStr, String bankType,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            SumDkReceiveMoneyBean sumDkReceiveMoney = dkClaimService.getSumDkReceiveMoney(user.getCoId(), searchStr, monthStr, bankType);
            List<DkReceiveBean> list = dkClaimService.getUnclaimedList(user, searchStr, monthStr, bankType, pageSize, pageNum);
            CommonPage<DkReceiveBean> dkReceiveCommonPage = CommonPage.restPage(list);
            dkReceiveCommonPage.setDkTotalMoney(sumDkReceiveMoney.getDkTotalMoney());
            dkReceiveCommonPage.setYrlTotalMoney(sumDkReceiveMoney.getYrlTotalMoney());
            return CommonResult.success(dkReceiveCommonPage,"获取可认领到款记录成功");
        } catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取可认领到款记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取可认领到款记录失败" + e.getMessage());
        }
    }

    //获取已订阅待认领到款记录清单
    @GetMapping("/getUnclaimedListSubs")
    public CommonResult getUnclaimedListSubs(String searchStr, String bankType,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<DkReceiveBean> list = dkClaimService.getUnclaimedListSubs(user, searchStr, bankType, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取可认领到款记录成功");
        } catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取已订阅往来单位可认领到款记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取已订阅往来单位可认领到款记录失败");
        }
    }

    //获取认领到款记录清单
    @GetMapping("/getClaimList")
    public CommonResult getClaimList(String flowStatus, String searchStr, String monthStr, String deptName,
                                     String startTime, String endTime, String receiveType, String businessType,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        if(BlankUtils.isBlank(flowStatus)){
            return CommonResult.failed("未收到流程状态(flowStatus)");
        }
//        if(BlankUtils.isBlank(abolishStatus)){
//            abolishStatus = "0";
//        }
        if(BlankUtils.isBlank(monthStr)){
            monthStr = null;
        }
        if(BlankUtils.isBlank(deptName)){
            deptName = null;
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            SumDkReceiveMoneyBean sumDkReceiveMoney = dkClaimService.getSumDkClaimMoney(flowStatus, searchStr, monthStr, deptName, startTime, endTime, receiveType, businessType, user);
            List<DkClaimBean> list = dkClaimService.getClaimList(flowStatus, searchStr, monthStr, deptName, startTime, endTime, receiveType, businessType, user, pageSize, pageNum);
            CommonPage<DkClaimBean> dkClaimBeanCommonPage = CommonPage.restPage(list);
            dkClaimBeanCommonPage.setTotal(Long.valueOf(sumDkReceiveMoney.getCount()));
            dkClaimBeanCommonPage.setYrlTotalMoney(sumDkReceiveMoney.getYrlTotalMoney());
            return CommonResult.success(dkClaimBeanCommonPage,"获取到款认领记录成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取到款认领记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取到款认领记录失败");
        }
    }

    //根据ID获取认领信息
    @GetMapping("/getClaimById")
    public CommonResult getClaimById(Integer claimId){
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.getClaimById(user, claimId);
        return commonResult;
    }

    //根据ID获取认领发票信息
    @GetMapping("/getClaimInvoiceById")
    public CommonResult getClaimInvoiceById(Integer claimInvoiceId){
        if(BlankUtils.isBlank(claimInvoiceId)){
            return CommonResult.failed("未收到认领-发票记录ID(claimInvoiceId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.getClaimInvoiceById(user, claimInvoiceId);
        return commonResult;
    }

    //根据ID获取认领合同信息
    @GetMapping("/getClaimContractById")
    public CommonResult getClaimContractById(Integer claimContractId){
        if(BlankUtils.isBlank(claimContractId)){
            return CommonResult.failed("未收到认领-合同记录ID(claimContractId)");
        }
        CommonResult commonResult = dkClaimService.getClaimContractById(claimContractId);
        return commonResult;
    }

    //根据ID获取认领任务信息
    @GetMapping("/getClaimTaskById")
    public CommonResult getClaimTaskById(Integer claimTaskId){
        if(BlankUtils.isBlank(claimTaskId)){
            return CommonResult.failed("未收到认领-任务记录ID(claimTaskId)");
        }
        CommonResult commonResult = dkClaimService.getClaimTaskById(claimTaskId);
        return commonResult;
    }

    //获取认领已关联发票清单
    @GetMapping("/getClaimInvoiceByClaimId")
    public CommonResult getClaimInvoiceByClaimId(Integer claimId){
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.getClaimInvoiceByClaimId(user, claimId);
        return commonResult;
    }

    //获取认领已关联合同清单
    @GetMapping("/getClaimContractByClaimId")
    public CommonResult getClaimContractByClaimId(Integer claimId){
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        CommonResult commonResult = dkClaimService.getClaimContractByClaimId(claimId);
        return commonResult;
    }

    //获取认领已关联任务清单
    @GetMapping("/getClaimTaskByClaimId")
    public CommonResult getClaimTaskByClaimId(Integer claimId){
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        CommonResult commonResult = dkClaimService.getClaimTaskByClaimId(claimId);
        return commonResult;
    }

    //获取认领流程清单
    @GetMapping("/getFlowByClaimId")
    public CommonResult getFlowByClaimId(Integer claimId){
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.getFlowByClaimId(user, claimId);
        return commonResult;
    }

    //驳回认领
    @RequestMapping("/rejectClaim")
    public CommonResult rejectClaim(Integer flowId, Integer claimId, String flowNotes){
        if(BlankUtils.isBlank(flowId)){
            return CommonResult.failed("未收到流程ID(flowId)");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.rejectClaim(user, flowId, claimId, flowNotes);
        logService.writeOperationLog(moduleName,commonResult);
        return commonResult;
    }

    //通过认领
    @RequestMapping("/approveClaim")
    public CommonResult approveClaim(Integer flowId, Integer claimId, String flowNotes){
        if(BlankUtils.isBlank(flowId)){
            return CommonResult.failed("未收到流程ID(flowId)");
        }
        if(BlankUtils.isBlank(claimId)){
            return CommonResult.failed("未收到认领ID(claimId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.approveClaim(user, flowId, claimId, flowNotes);
        logService.writeOperationLog(moduleName,commonResult);
        return commonResult;
    }


    //自动汇总金额
    @RequestMapping("/autoCalMoney")
    public CommonResult autoCalMoney(Integer claimId){
        return dkClaimService.autoCalMoney(claimId);
    }

    //获取到款类型字典表
    @GetMapping("/getReceiveTypeDict")
    public CommonResult getReceiveTypeDict(String searchStr){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return dkClaimService.getReceiveTypeDict(user, searchStr);
    }

    //获取归口部门字典表
    @GetMapping("/getResponsibleDeptDict")
    public CommonResult getResponsibleDeptDict(String searchStr){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return dkClaimService.getResponsibleDeptDict(user, searchStr);
    }

    //初次认领作废并返回ID
    @RequestMapping("/abolishClaim")
    public CommonResult abolishClaim(@RequestBody DkClaimCancel dkClaimCancel){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return dkClaimService.abolishClaim(user, dkClaimCancel);
    }

    //保存认领作废
    @RequestMapping("/saveAbolishClaim")
    public CommonResult saveAbolishClaim(@RequestBody DkClaimCancel dkClaimCancel){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return dkClaimService.saveAbolishClaim(user, dkClaimCancel);
    }

    //提交认领作废
    @RequestMapping("/submitAbolishClaim")
    public CommonResult submitAbolishClaim(@RequestBody DkClaimCancel dkClaimCancel){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.submitAbolishClaim(user, dkClaimCancel);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //重新提交认领作废
    @RequestMapping("/resubmitAbolishClaim")
    public CommonResult resubmitAbolishClaim(@RequestBody DkClaimCancel dkClaimCancel){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.resubmitAbolishClaim(user, dkClaimCancel);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }


    //取消认领作废
    @RequestMapping("/cancelClaimAbolish")
    public CommonResult cancelClaimAbolish(Integer abolishId){
        if(BlankUtils.isBlank(abolishId)){
            return CommonResult.failed("未收到作废记录ID(abolishId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.cancelClaimAbolish(user, abolishId);
        logService.writeOperationLog(moduleName,commonResult);
        return commonResult;
    }

    //获取认领作废数据清单
    @GetMapping("/getClaimAbolishDataList")
    public CommonResult getClaimAbolishDataList(String searchStr, String flowStatus,
                                                String monthStr, String deptName,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<DkClaimAbolishBean> list = dkClaimService.getClaimAbolishDataList(user, searchStr,
                    flowStatus, monthStr,
                    deptName, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取到款认领废除记录成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取到款认领废除记录失败，参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("获取到款认领废除记录失败");
        }
    }

    //获取单条认领作废数据
    @GetMapping("/getClaimAbolishDataById")
    public CommonResult getClaimAbolishDataList(Integer abolishId){
        if(BlankUtils.isBlank(abolishId)){
            return CommonResult.failed("未收到作废记录ID(abolishId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return dkClaimService.getClaimAbolishDataById(user, abolishId);
    }

    //获取认领作废流程
    @GetMapping("/getClaimAbolishFlow")
    public CommonResult getClaimAbolishFlow(Integer abolishId){
        if(BlankUtils.isBlank(abolishId)){
            return CommonResult.failed("未收到作废记录ID(abolishId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        return dkClaimService.getClaimAbolishFlow(user, abolishId);
    }

    //通过认领作废
    @RequestMapping("/approveClaimAbolish")
    public CommonResult approveClaimAbolish(Integer abolishId, String flowNote){
        if(BlankUtils.isBlank(abolishId)){
            return CommonResult.failed("未收到作废记录ID(abolishId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.approveClaimAbolish(user, flowNote, abolishId);
        logService.writeOperationLog(moduleName,commonResult);
        return commonResult;
    }

    //拒绝认领作废
    @RequestMapping("/rejectClaimAbolish")
    public CommonResult rejectClaimAbolish(Integer abolishId, String flowNote){
        if(BlankUtils.isBlank(abolishId)){
            return CommonResult.failed("未收到作废记录ID(abolishId)");
        }
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        CommonResult commonResult = dkClaimService.rejectClaimAbolish(user, flowNote, abolishId);
        logService.writeOperationLog(moduleName,commonResult);
        return commonResult;
    }

    //获取任务可关联合同号清单
    @GetMapping("/getClaimTaskContractList")
    public CommonResult getClaimTaskContractList(Integer claimTaskId, String searchStr,
                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        if(BlankUtils.isBlank(claimTaskId)){
            return CommonResult.failed("未收到认领-任务记录ID(claimTaskId)");
        }
        try{
            List<DkContractBean> list = dkClaimService.getClaimTaskContractList(claimTaskId, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list), "获取认领任务可关联合同成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            logService.writeErrorLog("获取认领任务可关联合同失败,参考原因"+e.getMessage(), moduleName);
            return CommonResult.failed("获取认领任务可关联合同失败");
        }

    }

    //认领任务关联合同
    @RequestMapping("/claimTaskRelateContract")
    public CommonResult claimTaskRelateContract(Integer claimTaskId, Integer contractId){
        if(BlankUtils.isBlank(claimTaskId)){
            return CommonResult.failed("未收到认领任务记录ID(claimTaskId)");
        }
        if(BlankUtils.isBlank(contractId)){
            return CommonResult.failed("未收到合同ID(contractId)");
        }
        CommonResult commonResult = dkClaimService.claimTaskRelateContract(claimTaskId, contractId);
        logService.writeOperationLog(moduleName,commonResult);
        return commonResult;
    }

    //发票-选择到款-已认领
    @GetMapping("/getDkClaimedList")
    public CommonResult getDkClaimedList(BigDecimal invMoney, String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<DkClaimedBean> list = dkClaimService.getDkClaimedList(invMoney, user, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list),"获取已认领列表成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            return CommonResult.failed("获取已认领记录失败");
        }
    }

    //获取到款认领业务类型（它需要根据当前操作人的角色判断是否展示相应条目）
    @GetMapping("/getDkBusinessType")
    public CommonResult getDkBusinessType(){
        User user;
        try{
            user = getLoginUser();
        } catch (Exception e){
            if(defaultUser){
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try{
            List<SysDictData> list = sysDictDataService.getDkBusinessType(user.getId());
            return CommonResult.success(list,"获取到款认领业务类型成功");
        }catch (Exception e){
            System.out.println(e.getMessage());
            return CommonResult.failed("获取到款认领业务类型失败");
        }
    }

    //商业承兑汇票：逾期名单列表
    @GetMapping("/getScYqmdList")
    public CommonResult getScYqmdList(String searchStr,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            if (defaultUser) {
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try {
            List<DwmdBean> list = dkClaimService.getScYqmdList(user, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list), "获取逾期名单列表成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return CommonResult.failed("获取逾期名单列表失败");
        }
    }

    //银行承兑汇票：白名单列表
    @GetMapping("/getYcBmdList")
    public CommonResult getYcBmdList(String searchStr,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        User user;
        try {
            user = getLoginUser();
        } catch (Exception e) {
            if (defaultUser) {
                user = testUser();
            } else {
                return CommonResult.failed("无法从token中获取用户信息");
            }
        }
        try {
            List<DwmdBean> list = dkClaimService.getYcBmdList(user, searchStr, pageSize, pageNum);
            return CommonResult.success(CommonPage.restPage(list), "获取白名单列表成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return CommonResult.failed("获取白名单列表失败");
        }
    }

}
